package com.krm;

//import com.sun.tools.javac.util.StringUtils;

import java.util.List;

public class ToOracleSql extends ToSql{


    /**
     * 转成 sql create table
     * @param exceBeanList
     * @return
     */
    @Override
    public String toSql(List<ExcelBean> exceBeanList){
        StringBuilder sb = new StringBuilder();

        if(exceBeanList==null||exceBeanList.size()<=0){
            return "Excel有问题未能解析出数据";
        }
        String comment="";
        for(ExcelBean eb : exceBeanList){
            String pkid = "";
            comment = "";
            if(eb ==null){
                continue;
            }

            sb.append("\r\n");
            sb.append("\r\n");


            sb.append(" DROP TABLE "+eb.getTableName() +";\r\n");
            sb.append(" CREATE TABLE "+eb.getTableName() +"(");
            List<TableBean> listTableBean =  eb.getTableBean();
            int conut = 0;

            for(TableBean tb : listTableBean) {
                if (tb == null) {
                    continue;
                }

                String clunmeName = tb.getClunmeName().trim();
                if("".equals(clunmeName)||clunmeName==null){
                    continue;
                }

                sb.append("  "+clunmeName);
                if(tb.getType().contains("(")||"INTEGER".equalsIgnoreCase(tb.getType().trim())){
                    sb.append("  " + tb.getType().trim());
                }else {
//                    System.out.println(eb.getTableName()+"---------------"+tb.getClunmeName()+"----"+tb.getType().trim()+"-----"+tb.getLength().trim());
                    sb.append("  " + tb.getType().trim() + "(" + Integer.parseInt(tb.getLength().trim()) + "");

                    if (!"".equals(tb.getRecision() ) && tb.getRecision() != null) {
                        sb.append("," + Integer.parseInt(tb.getRecision().trim()));
                    }
                    sb.append(") ");
                }
                if ( !"".equals(tb.getIsPkid())&&tb.getIsPkid()!=null ){
                    sb.append("  not null ");
                }

                if(conut!=(listTableBean.size()-1)){
                    sb.append(",\r\n");

                }

                if(!"".equals(tb.getIsPkid())&&tb.getIsPkid()!=null){
                    pkid+= tb.getClunmeName().trim()+",";
                }
//                COMMENT ON COLUMN FDM_CUST_ENT.test IS '茜';
                String tbCommt = tb.getCommt().trim();
                if(!"".equals(tbCommt)&&tbCommt!=null) {
                    comment += " COMMENT ON COLUMN " + eb.getTableName() + "." + tb.getClunmeName();
                    comment += " IS '" + tbCommt + "';\r\n";

                }



//                TEST_KEY
//                add constraint PK_TEST_KEY primary key (A1, A2, B1)
                conut++;
            }

            sb.append(");\r\n");
            if(pkid!=""&&pkid!=null){
//                pkid = pkid.trim();
                pkid = pkid.substring(0,pkid.length()-1);
                sb.append("alter table "+eb.getTableName() +" add constraint PK_"+eb.getTableName()+" primary key ("+pkid+");\r\n");
            }
             sb.append(comment);

            String tableNameComment = " COMMENT ON TABLE  "+eb.getTableName()+" IS '" +eb.getTableCommt()+"';\r\n" ;
            sb.append(tableNameComment);
        }

        sb.append("\r\ncommit;");

        return sb.toString();
    }



}
